package com.hyh.web;

import com.hyh.bean.Grade;
import com.hyh.bean.Student;
import com.hyh.service.GradeService;
import com.hyh.service.StudentService;
import com.hyh.service.impl.GradeServiceImpl;
import com.hyh.service.impl.StudentServiceImpl;
import com.hyh.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/studentServlet")
public class StudentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("insert".equals(method)){
            insertStu(req, resp);
        }else if ("delete".equals(method)){
            deleteStu(req, resp);
        }else if ("update".equals(method)) {
            updateStu(req, resp);
        }else if ("findById".equals(method)) {
            findById(req, resp);
        }else {
            selectStu(req, resp);
        }
    }
    /**
     * 新增学员
     */
    protected void insertStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取新增参数
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");
        //调取service
        StudentService service = new StudentServiceImpl();
        //封装参数
        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introduction);
        int i = service.insertStudent(student);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0){
            writer.println("<script>alert('新增成功');location.href='/Educational/student/studentServlet';</script>");
        }else {
            writer.println("<script>alert('新增失败');location.href='/Educational/student/getGradeList';</script>");
        }
    }
    /**
     * 删除学员
     */
    protected void deleteStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        StudentService service = new StudentServiceImpl();
        int i = service.delStudent(sid);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0){
            writer.println("<script>alert('删除成功');location.href='/Educational/student/studentServlet';</script>");
        }else {
            writer.println("<script>alert('删除失败');location.href='/Educational/student/studentServlet';</script>");
        }
    }

    /**
     * 修改学员
     */
    protected void updateStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取新增参数
        String sid = req.getParameter("sid");
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");
        Student student = new Student();
        student.setStuId(Integer.parseInt(sid));
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introduction);
        StudentService service = new StudentServiceImpl();
        int i = service.updateStudent(student);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0){
            writer.println("<script>alert('修改成功');location.href='/Educational/student/studentServlet';</script>");
        }else {
            writer.println("<script>alert('修改失败');location.href='/Educational/student/studentServlet?method=findById&sid="+sid+"';</script>");
        }
    }
    /**
     * 查询学员
     */
    protected void selectStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 获取参数
        //1.1 模糊查询参数
        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        //1.2 分页数据 limit 开始位置，显示条数
        //页码值(当前页码值)
        String pageIndex = req.getParameter("pageIndex");
        //如果页面没有传入pageIndex的值，则认为默认查询首页
        int index = pageIndex == null?1:Integer.parseInt(pageIndex);
        //2. 调取service方法
        StudentServiceImpl service = new StudentServiceImpl();
        PageUtil pageUtil = new PageUtil();
        int usex = (sex == null || sex.length() == 0?-1:Integer.parseInt(sex));
        List<Student> students = service.getStudents(stuname,stuno,usex,index,pageUtil.getPageSize());
        //获取总页数=总条数%每页显示的条数>0?总条数/每页显示条数+1:总条数/每页显示条数
        int total = service.total(stuname, stuno, usex);//总条数
        pageUtil.setTotal(total);
        //3. 跳转页面
        //如果后台想给前台传数据，是一定要在后台存值的
        pageUtil.setDateList(students);
        pageUtil.setPageIndex(index);
        //存储模糊查询条件数据
        req.setAttribute("stuname",stuname);
        req.setAttribute("stuno",stuno);
        req.setAttribute("sex",sex);
        //存储分页数据
        req.setAttribute("page",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
    /**
     * 主键查询
     */
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        StudentService service = new StudentServiceImpl();
        Student student = service.findById(Integer.parseInt(sid));
        GradeService gradeService = new GradeServiceImpl();
        List<Grade> gradeList = gradeService.getList();
        req.setAttribute("glist",gradeList);
        req.setAttribute("slist",student);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }
}
